<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/14
 * Time: 02:02 PM
 */
class PHZ_User
{

    /**
     * 给某个用户增加一个BP牌
     * @param $room_info      房间信息
     * @param $user_id        用户ID
     * @param $card_number    牌号
     * @param $db_connect     数据库链接标识
     */
    public static function __user_add_bad_pai( $room_info , $user_id , $card_number , $db_connect ){
        $where             = "uid = '". $user_id ."' and rid = '". $room_info['room_id'] ."' and jid = '". $room_info['room_js'] ."'";
        $user_card_info    = $db_connect->select('*')->from('tt_room_card')->where( $where )->row();
        $user_bp           = $user_card_info['bp'] . $card_number . ",";
        $db_connect->update('tt_room_card')->cols( array('bp'=> $user_bp ) )->where( $where )->query();
        return true;
    }

    /**
     * 对pass_peng进行检查；查询是否存在可碰却未碰的牌？
     * @param $room_info    房间信息
     * @param $card_number  牌号
     * @param $db_connect   数据库链接
     * @return bool
     */
    public static function __Peng_card_is_Out( $room_info , $user_id , $card_number , $db_connect ){
        $where             = "uid = '". $user_id ."' and rid = '". $room_info['room_id'] ."' and jid = '". $room_info['room_js'] ."'";
        $user_card_info    = $db_connect->select('*')->from('tt_room_card')->where( $where )->row();
        $pass_peng_info    = explode(",", $user_card_info['pass_peng'] );
        foreach ( $pass_peng_info as $key => $value ){
            if( $card_number == $value ){
                return false;
                break;
            }
        }
        return true;
    }

    /**
     * 查询某个用户是否吃过章了
     * 1.手打牌检测因为在检测之前就已经添加了BP了，所以这里的BP需要减去当前的这个再做检测
     * @param $room_info      房间信息
     * @param $user_id        用户ID
     * @param $card_number    牌号
     * @param $run_action     执行吃的动作（手打牌检测还是摸牌检测）
     * @param $db_connect     数据库链接标识
     * @return false || true
     */
    public static function __Eat_card_is_Out( $room_info , $user_id , $card_number , $db_connect ){
        $where             = "uid = '". $user_id ."' and rid = '". $room_info['room_id'] ."' and jid = '". $room_info['room_js'] ."'";
        $user_card_info    = $db_connect->select('*')->from('tt_room_card')->where( $where )->row();
        $pass_chi_info     = explode(",", $user_card_info['pass_chi'] );
        foreach ( $pass_chi_info as $key => $value ){
            if( $card_number == $value ){
                return false;
                break;
            }
        }
        return true;
    }

    /**
     * 查询某个用户的上一个用户是谁？
     * @param $room_info
     * @param $user_id
     */
    public static function __get_user_Up_userID( $room_info , $user_id ){
        if( $room_info['room_player_1'] == $user_id ){
            return $room_info['room_player_3'];
        }
        if( $room_info['room_player_2'] == $user_id ){
            return $room_info['room_player_1'];
        }
        if( $room_info['room_player_3'] == $user_id ){
            return $room_info['room_player_2'];
        }
    }

    /**
     * 查询某个用户的下一个用户是谁？
     * @param $room_info   房间信息
     * @param $uid         当前操牌者（可能是出牌者，也可能是跑牌的人）
     * @return int         返回下一个摸牌人的用户ID
     */
    public static function __get_user_Next_userID( $room_info , $uid ){
        if( $room_info['room_player_1'] == $uid ){
            return $room_info['room_player_2'];
        }
        if( $room_info['room_player_2'] == $uid ){
            return $room_info['room_player_3'];
        }
        if( $room_info['room_player_3'] == $uid ){
            return $room_info['room_player_1'];
        }
    }



    /**
     * 获取一个用户当前在房间里的第几个位置上
     * @param $room_info
     * @param $uid
     */
    public static function __get_user_Room_Index( $room_info , $uid ){
        if( $room_info['room_player_1'] == $uid ){
            return 1;
        }else if( $room_info['room_player_2'] == $uid ){
            return 2;
        }else if( $room_info['room_player_3'] == $uid ){
            return 3;
        }
    }

}